# coding=utf-8

import requests
from lxml import etree
import json
import copy


# 请求网页
def getHtml(url, text=True):
    baseUrl = ''
    if url == '':
        baseUrl = r'http://www.mca.gov.cn/article/sj/xzqh/2019/201908/201908271607.html'
    else:
        baseUrl = url

    headers = {
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
        'Cookie': '_gscu_190942352=68258124aw3tya48; _gscbrs_190942352=1'
    }
    getPage = requests.get(url=baseUrl, headers=headers)

    if text == True:
        return getPage.text
    else:
        return getPage.content


# 读取保存的文件内容
def readHtml(path):
    if path == '':
        return None
    with open(path, 'r', encoding='utf-8') as fp:
        return fp.read()


# 保存字典为json数据
def saveDict2json(dic):
    data = json.dumps(dic)
    print(data)
    f = open('admin_data.json', 'w', encoding='utf-8')
    f.write(data)
    f.close()


# 划分同一省份的城市
def toClassify(dic):
    itme = {}
    for i in dic.keys():
        nn = i[:2]
        if nn not in itme.keys():
            itme[nn] = dict()
            itme[nn][i] = dic.get(i)
        if nn in itme.keys():
            itme[nn][i] = dic.get(i)

    # for i in itme.values():
    #     print(i)

    return list(itme.values())


# 将分开的数据，将省提取出来
def getProvince(dic_li):
    min_key = 999999  # 城市编号为6位
    data_li = []
    for data in dic_li:
        for k, v in data.items():
            min_key = min_key if min_key < int(k) else int(k)
        temp = {}
        # 添加 省数据
        min_key = str(min_key)
        temp['province'] = {min_key: data.get(min_key)}
        data.pop(min_key)
        temp['cityli'] = data  # 添加省级一下城市
        min_key = 999999  # 对比完成后，将变量重置
        data_li.append(temp)
    return data_li


# 将同一省份的城市细分
def toClassify_city(pro):
    itme = {}
    dic = pro.get('cityli')
    for i in dic.keys():
        nn = i[:4]
        if nn not in itme.keys():
            itme[nn] = dict()
            itme[nn][i] = dic.get(i)
        if nn in itme.keys():
            itme[nn][i] = dic.get(i)

    city_li = []
    for line in itme.values():
        line_d = copy.deepcopy(line)
        # print('line_d', line_d)
        for k, v in line.items():
            temp = {}
            if k[-2:] == '00':
                cc = {}
                cc[k] =v
                temp['city'] = cc
                line_d.pop(k)
                temp['areas'] = line_d
                break
            else:
                temp = {'city': line_d}
        city_li.append(temp)
    pro['cityli'] = city_li

    # print(pro)
    return pro


# 格式化打印省县数据
def printCitys(admin_li):
    for line in admin_li:
        print(line.get('province'))
        # print('   ', line)
        for ctline in line.get('cityli'):
            if len(ctline.get('city')) == 1:
                print('   ', ctline.get('city'))
                if ctline.get('areas', ''):
                    for area in ctline.get('areas').items():
                        print('   ' * 2, area)
            else:
                for ct in ctline.get('city').items():
                    print('   ', ct)


if __name__ == '__main__':
    # 读取文件
    html = etree.HTML(readHtml('./201908271607.html'))
    # 过滤数据
    code = html.xpath('//div/table/tr/td[2]/text()')
    city = html.xpath('//div/table/tr/td[3]/text()')
    # 保存字典
    contoy_dic = {}
    for k, v in zip(code, city):
        contoy_dic[k] = v

    # 打印 -- 去掉标题
    contoy_dic.pop('行政区划代码')
    # print(contoy_dic)

    # 数据转换并保存 -- 运行一次，可以保存完整的数据
    # saveDict2json(contoy_dic)

    # 城市分类
    province_li = toClassify(contoy_dic)

    # 将省级数据提取
    province_li = getProvince(province_li)

    # for i in province_li:
    #     print(i)
    #     print('cityli  ', i.get('cityli'))
    #     print('--'*23)
    # print('=='*23)

    # 最终的数据
    admin_li = list()

    # 城市细分
    for pro in province_li:
        admin_li.append(toClassify_city(pro))

    # 保存最终数据
    # saveDict2json(admin_li)


    printCitys(admin_li)